﻿using System;
using System.Collections.Generic;
using ShopB2C.Enums;

namespace ShopB2C.Parameters {
    // 分类完整路径 - 特性搜索 - 显示方式 - 排序字段 - 排序方向 - 页码
    // (345,123,234)-(456,980,345)-1-0-0-1
    /// <summary>
    /// 产品展示页参数组合
    /// </summary>
    [Serializable]
    public class ProductParam {
        public ProductParam() {
            this.Classify = new List<int>() { 0 };
            this.PropertyVal = new List<int>() { 0 };
            this.ShowType = ProductShowType.List;
            this.SortFieldName = ProductSortFieldNames.SaleDateTime;
            this.SortDescription = SortDescriptions.DESC;
            this.PageIndex = 1;
        }
        /// <summary>
        /// 当前分类
        /// </summary>
        public List<int> Classify { get; set; }
        /// <summary>
        /// 当前分类父分类
        /// </summary>
        public List<int> PropertyVal { get; set; }
        /// <summary>
        /// 产品显示方式
        /// </summary>
        public ProductShowType ShowType { get; set; }
        /// <summary>
        /// 排序字段
        /// </summary>
        public ProductSortFieldNames SortFieldName { get; set; }
        /// <summary>
        /// 排序方向
        /// </summary>
        public SortDescriptions SortDescription { get; set; }
        /// <summary>
        /// 页码
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 参选还原成字符串
        /// </summary>
        /// <returns>string</returns>
        public override string ToString() {
            return string.Format("({0})-({1})-{2}-{3}-{4}-{5}",
                string.Join(",", this.Classify.ConvertAll<string>(p => { return p.ToString(); }).ToArray()),
                string.Join(",", this.PropertyVal.ConvertAll<string>(p => { return p.ToString(); }).ToArray()),
                (int)this.ShowType,
                (int)this.SortFieldName,
                (int)this.SortDescription < 1 ? 0 : 1,
                this.PageIndex);
        }
        /// <summary>
        /// 克隆一个参数类
        /// </summary>
        /// <param name="pp">被克隆的参数类</param>
        /// <returns>克隆的参数类</returns>
        public ProductParam Clone() {
            List<int> cList = new List<int>(this.Classify.Count);
            cList.AddRange(this.Classify);
            List<int> pvList = new List<int>(this.PropertyVal.Count);
            pvList.AddRange(this.PropertyVal);

            return new ProductParam() {
                Classify = cList,
                PropertyVal = pvList,
                ShowType = this.ShowType,
                SortFieldName = this.SortFieldName,
                SortDescription = this.SortDescription,
                PageIndex = this.PageIndex
            };
        }
        /// <summary>
        /// 连接分类, 指定分隔符
        /// </summary>
        /// <param name="seperater">分隔符</param>
        /// <returns>string</returns>
        public string JoinClassify(string seperater) {
            return string.Join(seperater, 
                this.Classify.ConvertAll<string>(p => { return p.ToString(); }).ToArray());
        }
        /// <summary>
        /// 连接特性, 指定分隔符
        /// </summary>
        /// <param name="seperater">分隔符</param>
        /// <returns>string</returns>
        public string JoinPropertyVal(string seperater) {
            return string.Join(seperater,
                this.PropertyVal.ConvertAll<string>(p => { return p.ToString(); }).ToArray());
        }
        /// <summary>
        /// 清除指定的特性选择
        /// </summary>
        /// <param name="pos">指定的位置</param>
        /// <returns>string</returns>
        public string ClearPropertyVal(params int[] pos) {
            if (pos == null) {
                for (int i = 0; i < this.PropertyVal.Count; i++) {
                    this.PropertyVal[i] = 0;
                }
            }
            else {
                int count = this.PropertyVal.Count;
                foreach (int i in pos) {
                    if (i < count) {
                        this.PropertyVal[i] = 0;
                    }
                }
            }
            return this.ToString();
        }
        /// <summary>
        /// 清除指定的分类选择
        /// </summary>
        /// <param name="pos">指定的位置</param>
        /// <returns></returns>
        public string ClearClassify(params int[] pos) {
            if (pos == null) {
                this.Classify.Clear();
                this.Classify.Add(0);
            }
            else {
                int count = this.Classify.Count;
                foreach (int i in pos) {
                    if (i < count) {
                        this.Classify.RemoveAt(i);
                    }
                }
            }
            return this.ToString();
        }
    }
}
